package com.study.www.web;

import com.alibaba.fastjson.JSON;
import com.study.www.entity.Dept;
import com.study.www.excception.ParamException;
import com.study.www.excception.UrlException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : Administrator.zhuyanpeng
 * @date : 2019/10/21 - 17:58
 */
//@RequestMapping("/dept")
@Controller
public class DeptController {

    public static Map<String, Dept> map = new HashMap<String, Dept>(10);

    @GetMapping("/demo")
    public String getDept(@RequestParam String id,
                          ModelMap modelMap) {
        id = id.trim();
        if (id.length() > 26) {
            throw new UrlException("100", "id不合法", "paramError");
        }
        modelMap.put("dept",map.get(id));
        return "dept/info";
    }

    @GetMapping("/param")
    @ResponseBody
    public String getParam(@RequestParam String id) {
        id = id.trim();
        if (id.length() > 26) {
            throw new ParamException("100", "id不合法");
        }
        return JSON.toJSONString(map.get(id));
    }

    @GetMapping("/500")
    public String getServerError(@RequestParam Integer id) {
        int i = 500 / id;
        return "dept/info";
    }
}
